package com.tl.libpay.ali;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import com.tl.libmanager.PayEntrance;
import com.tl.libpay.ui.PayHelper;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.Map;

/**
 * @author JongSung
 * @Description 阿里支付逻辑
 * @date 2017/8/25 0022
 */
public class PayAli {
    private static final int SDK_PAY_FLAG = 1;
    private Reference<Activity> activity;
    private PayEntrance.PayListener listener;

    public PayAli(Activity activity) {
        this.activity = new WeakReference<>(activity);
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        listener.onSuccess(PayHelper.PAY_TYPE_ALI);
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        listener.onFailed(PayHelper.PAY_TYPE_ALI, payResult.getResult(), payResult.getResultStatus());
                    }
                    break;
                }
                default:
                    break;
            }
            return false;
        }
    });

    public void pay(final String orderInfo, PayEntrance.PayListener aliPayListener) {
        this.listener = aliPayListener;
        if (TextUtils.isEmpty(orderInfo)) {
            if (this.listener != null) {
                this.listener.onFailed(PayHelper.PAY_TYPE_ALI, "params should be not null", "-1000");
            }
            return;
        }
        final Activity act = activity.get();
        if (act == null) {
            if (this.listener != null) {
                this.listener.onFailed(PayHelper.PAY_TYPE_ALI, "the window being attached dismissed or not exist", "-1001");
            }
            return;
        }
//        notify_url=https%253A%252F%252Fnc.cn2401.com%253A18443%252Fcn2401%252Fshop%252Falipay%252Fcallbacks.do&
//        final orderInfos = "app_id=2017081608219585&biz_content=%257B%2522total_amount%2522%253A%25221.0%2522%252C%2522body%2522%253A%25222401_%25E6%259C%258D%25E5%258A%25A1%25E8%25B4%25B9%2522%252C%2522timeout_express%2522%253A%252230m%2522%252C%2522product_code%2522%253A%2522QUICK_MSECURITY_PAY%2522%252C%2522subject%2522%253A%25222401_%25E6%259C%258D%25E5%258A%25A1%25E8%25B4%25B9%2522%252C%2522seller_id%2522%253A%25222088721841060241%2522%252C%2522out_trade_no%2522%253A%25221504592331362%2522%257D&charset=utf-8&format=json&method=alipay.trade.app.pay&sign=OsDqwyUwNQkNB0LrCxgtyvqofB4MiMqL2AW4lI4wDO0reR%252FMdRg78xy7bvSBqkfDYashu%252FmW0lqRx5j3V1TRKkIfO%252B3c5SeN7NQBVwkFgX4MuyC8%252FOJDDDIF8ooy3SYdvRcH%252FzPDx%252FEfzQxKDg3Bt4azKweFBMtXFf1vkvzL15M%253D&timestamp=2017-09-05%2B14%253A18%253A51&version=1.0&&sign=OsDqwyUwNQkNB0LrCxgtyvqofB4MiMqL2AW4lI4wDO0reR%2FMdRg78xy7bvSBqkfDYashu%2FmW0lqRx5j3V1TRKkIfO%2B3c5SeN7NQBVwkFgX4MuyC8%2FOJDDDIF8ooy3SYdvRcH%2FzPDx%2FEfzQxKDg3Bt4azKweFBMtXFf1vkvzL15M%3D";
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                PayTask aliPay = new PayTask(act);
                Map<String, String> result = aliPay.payV2(orderInfo, true);
                Message message = Message.obtain();
                message.what = SDK_PAY_FLAG;
                message.obj = result;
                handler.sendMessage(message);
            }
        };
        if (listener != null) {
            listener.startPay(PayHelper.PAY_TYPE_ALI);
        }
        Thread payThread = new Thread(runnable);
        payThread.start();
    }

}
